<template>
	<div class="sw_system_container">
		<div class="sw_query_form" v-if="$isHasAuth(topButton)">
			<div class="button" v-hasPermi="'d8e20bcaea98466b9950765c709b4f61'">
				<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenForm()">新增公告</el-button>
			</div>
			<el-form size="small" inline v-hasPermi="'65c10022c28a475fa2561f89393fba17'">
				<el-form-item>
					<el-input v-model="queryData.title" placeholder="请输入标题" clearable class="header-form-inputWidth" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="queryTableData">搜索</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="sw_table_container" v-loading="loading">
			<el-table size="medium" :data="tableData" height="100%">
				<el-table-column prop="title" label="标题" min-width="240">
					<template slot-scope="{ row }">
						<template v-if="$isHasAuth(['971a7252fcdc412d8cb508dfc8441fc8'])">
							<router-link :to="`/announcement/${row.id}`">{{ row.title }}</router-link>
						</template>
						<template v-else>
							{{ row.title }}
						</template>
					</template>
				</el-table-column>
				<el-table-column prop="releaseTime" label="发布时间" min-width="140"></el-table-column>
				<el-table-column prop="createName" label="创建者" min-width="120"></el-table-column>
				<el-table-column prop="createTime" label="创建时间" min-width="160"></el-table-column>
				<el-table-column label="操作" width="130" v-if="$isHasAuth(tableButton)">
					<template slot-scope="{ row }">
						<template v-if="row.del === '1'">
							<el-button type="text" size="medium" v-hasPermi="'d49cff8441a3446290c22f619509f8cb'" @click="handlePublishData(row, 2)">下架</el-button>
						</template>
						<template v-if="row.del === '2'">
							<el-button type="text" size="medium" v-hasPermi="'d49cff8441a3446290c22f619509f8cb'" @click="handlePublishData(row, 1)">发布</el-button>
						</template>
						<el-button type="text" size="medium" v-hasPermi="'4b1dacb13f2b4756ba4ea7ccff3651b9'" @click="handleOpenForm(row.id)" :disabled="row.del !== '2'">
							编辑
						</el-button>
						<el-button type="text" size="medium" v-hasPermi="'9ea71b6703af46898060896b0cf5f4b9'" class="danger" @click="handleDeleteData(row.id)">删除</el-button>
					</template>
				</el-table-column>
				<div slot="empty" class="emptyBg">
					<el-empty :image-size="200"></el-empty>
				</div>
			</el-table>
		</div>
		<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData"></sw-pagination>
		<announcement-form v-model="formDialog" :data-id="dataId" @refresh="queryTableData"></announcement-form>
	</div>
</template>
<script>
import moment from 'moment';
import announcementForm from './form';
import { tbSysJournalismQuery, tbSysJournalismDelete, tbSysJournalismUpdateDel } from '@/api/system/tbSysJournalism';
export default {
	components: { announcementForm },
	data() {
		return {
			loading: false,
			queryData: { page: 1, limit: 15 },
			dataCount: 0,
			tableData: [],
			dataId: '',
			formDialog: false
		};
	},
	computed: {
		topButton() {
			return ['d8e20bcaea98466b9950765c709b4f61', '65c10022c28a475fa2561f89393fba17'];
		},
		tableButton() {
			return ['d49cff8441a3446290c22f619509f8cb', '4b1dacb13f2b4756ba4ea7ccff3651b9', '9ea71b6703af46898060896b0cf5f4b9'];
		}
	},
	mounted() {
		this.queryTableData();
	},
	methods: {
		//获取通知公告数据
		async getTableData() {
			let params = { ...this.queryData };
			let result = await tbSysJournalismQuery(params).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.dataCount = parseInt(result.count);
				this.tableData = result.data || [];
			}
		},

		//格式化数据
		formatTableData(arr) {
			return arr.map((item) => {
				let obj = { ...item };
				obj['releaseTime'] = moment(item.releaseTime).format('YYYY-MM-DD');
				return obj;
			});
		},

		//查询表格数据
		queryTableData() {
			this.loading = true;
			this.getTableData();
		},

		//打开表单
		handleOpenForm(id) {
			this.dataId = id;
			this.formDialog = true;
		},

		//查看详情
		handleLookDetail(id) {
			this.$router.push(`/announcement/${id}`);
		},

		//发布/下架公告
		async handlePublishData(data, del) {
			let params = { id: data.id, del };
			let result = await tbSysJournalismUpdateDel(params).then((res) => res.data);
			if (result.code === '0') {
				if (del === 1) {
					this.$showCustomMessage('success', `发布公告成功`);
				} else if (del === 2) {
					this.$showCustomMessage('success', `下架公告成功`);
				}
				this.queryTableData();
			}
		},

		//删除公告
		handleDeleteData(id) {
			let that = this;
			this.$confirm('此操作将永久删除该公告, 是否继续?', '删除提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.deleteDataRequest(id);
						instance.confirmButtonLoading = false;
					}
					done();
				}
			}).catch(() => {});
		},

		//删除数据请求
		async deleteDataRequest(id) {
			let result = await tbSysJournalismDelete(id).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `删除公告成功`);
				this.queryTableData();
			}
		}
	}
};
</script>
<style scoped>
.announcement {
}
</style>
